package com.z.relam;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @ProjectName: springboot-08-shiro
 * @Package: realm
 * @ClassName: CustomerRealm
 * @author: Arisapoke
 * @date: 2021/2/2 17:26
 * @Version: 1.0
 */
public class CustomerRealm extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       //获取身份信息的用户名
        String principal = (String) authenticationToken.getPrincipal();
        System.out.println(principal);
        if ("chenhui".equals(principal)){
            //参数1是返回用户名 参数2返回用户密码 参数3返回当前用户的realm，当前通过类自身调用获取
            return new SimpleAuthenticationInfo("chenhui","123456",this.getName());
        }
        return null;
    }
}
